<template>
  <div class="app-container">
    <el-form :model="searchForm" ref="searchFormRef" size="small" inline v-show="showSearch">
      <el-form-item label="岗位编码" prop="like.pos_key">
        <el-input v-model="searchForm.like.pos_key" placeholder="请输入岗位编码" clearable/>
      </el-form-item>
      <el-form-item label="岗位名称" prop="like.name">
        <el-input v-model="searchForm.like.name" placeholder="请输入岗位名称" clearable/>
      </el-form-item>
      <el-form-item label="状态" prop="where.status">
        <el-select v-model="searchForm.where.status" placeholder="岗位状态" clearable class="w-130px">
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mb-8px reset">
      <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd" v-hasPerm="['system:post:add']">新增</el-button>
      <el-button type="success" plain icon="el-icon-edit" :disabled="ids.length !== 1" @click="handleEdit" v-hasPerm="['system:post:edit']">修改</el-button>
      <el-button type="danger" plain icon="el-icon-delete" :disabled="!ids.length" @click="handleDelete" v-hasPerm="['system:post:delete']">删除</el-button>
      <el-button type="warning" plain icon="el-icon-download" @click="handleExport" v-hasPerm="['system:post:export']">导出</el-button>
      <right-toolbar :showSearch.sync="showSearch" @refresh="getList"/>
    </div>

    <el-table v-loading="loading" :data="listData" @selection-change="handleSelection">
      <el-table-column type="selection" align="center" width="55" />
      <el-table-column label="编号" align="center" prop="id" width="60"/>
      <el-table-column label="岗位编码" align="center" prop="pos_key" />
      <el-table-column label="岗位名称" align="center" prop="name" />
      <el-table-column label="岗位排序" align="center" prop="list_sort" />
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="{ row }">
          <dict-tag :options="dict.type.sys_normal_disable" :value="row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="create_time" width="150">
        <template v-slot="{ row }">
          <span>{{ parseTime(row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template v-slot="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(row)" v-hasPerm="['system:post:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)" v-hasPerm="['system:post:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize" @pagination="getList"/>
    <PostForm v-model="formShow" :module-name="moduleName" :id="formId" @submit="getList" v-if="formShow"/>
  </div>
</template>

<script>
import { listPostApi, delPostApi, exportPostApi } from "@/api/system/post";
import PostForm from './form.vue'

export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  components: { PostForm },
  data() {
    return {
      moduleName: '岗位',
      loading: true,
      showSearch: true,
      ids: [],
      formId: null,
      formShow: false,
      listData: [],
      total: 0,
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        like: { pos_key: undefined, name: undefined },
        where: { status: undefined },
        orderByColumn: 'list_sort'
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listPostApi(this.searchForm).then(res => {
        this.listData = res.list
        this.total = res.total
        this.loading = false
      })
    },
    reset() {
      this.resetForm('searchFormRef')
      this.search()
    },
    search() {
      this.searchForm.pageNum = 1
      this.getList();
    },
    handleSelection(selection) {
      this.ids = selection.map(item => item.id)
    },
    handleAdd() {
      this.formId = null
      this.formShow = true
    },
    handleEdit(row) {
      this.formId = row?.id || (this.ids.length ? this.ids[0] : '')
      if (!this.formId) return this.$modal.msgError('参数缺失')
      this.formShow = true
    },
    handleDelete(row) {
      const postIds = row?.id || this.ids.join(',')
      if (!postIds) return this.$modal.msgWarning('未选择数据')
      this.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(() => {
        return delPostApi(postIds)
      }).then((res) => {
        this.getList()
        this.$modal.msgSuccess(res.msg || '删除成功')
      }).catch(() => {})
    },
    handleExport() {
      exportPostApi(this.searchForm)
    }
  }
};
</script>
